/* Filename: 2-有名管道.c
 * Description:
 * Last modified: humble 2020-11-02 23:18
 * $ gcc -Wall 2-有名管道.c -o 2-有名管道
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>


static void usage(void)
{
#define USAGE "Usage:\n\
  Helloworld\n"
    printf(USAGE);
}

int main(int argc, char **argv)
{
    int ret = -1;

    if(argc != 1){ fprintf(stderr, "argc !=1"); usage(); exit(1); }

    /*
    //创建有名管道（文件）
    int ret = mkfifo("fifo.tmp", 00400 | 00200);
    if (-1 == ret) { perror("mkfifo()"); exit(1); }
    */

    //默认是阻塞的
    int fd = open("fifo.tmp", O_WRONLY);
    if (0 > fd) { perror("open()"); exit(1); }

    char buf[32] = {0};
    while(1){
        scanf("%s", buf);
        ret = write(fd, buf, strlen(buf));
        if (-1 == ret) { perror("write()"); exit(1); }

        if(!strcmp(buf, "bye")){ break; }

        memset(buf, 0, sizeof(buf));
    }

    return 0;
}
